package com.wilmer.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.wilmer.bean.LoginBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * <p>获取小程序用户openid组件</p>
 * @author hans
 */
@Component
public class OpenIdUtil {

    @Value("${wechat.app_login_url}")
    private  String app_login_url;
    @Value("${wechat.app_id}")
    private  String app_id;
    @Value("${wechat.app_secret}")
    private  String app_secret;
    @Value("${wechat.grant_type_login}")
    private  String grant_type_login;
    public LoginBean getOpenId(String js_code){

        HashMap<String, Object> loginMap = new HashMap<>();
        loginMap.put("appid",app_id);
        loginMap.put("secret",app_secret);
        loginMap.put("grant_type_login",grant_type_login);
        loginMap.put("js_code",js_code);

        String res = HttpUtil.get(app_login_url,loginMap);
        LoginBean loginBean = JSONUtil.toBean(res, LoginBean.class);
        return loginBean;
    }

}
